package cn.wolfcode.luowowo.wesite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.until.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.vo.StatisVo;
import cn.wolfcode.luowowo.wesite.annotation.UserParam;
import cn.wolfcode.luowowo.wesite.until.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2019/8/11.
 */

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;


    @Reference
    private IStrategyTagService strategyTagService;


    //评论
    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    //推荐攻略
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;


    //攻略的首页
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //共享推荐攻略的前五
        model.addAttribute("commends",strategyCommendService.getTop5());
        //同zet查询出来所有的Vo对象
        List<StrategyStatisVO> list=strategyStatisRedisService.getAllVOByZet();
      /* for (StrategyStatisVO vs:list){
            System.out.println(list);
        }*/


        List<StrategyStatisVO> abroadCds=new ArrayList<>();//海外攻略排行的集合
        List<StrategyStatisVO> unabroadCds=new ArrayList<>();//国内攻略的排行的集合
        for(StrategyStatisVO vo:list){
            //国内和海外都满足为10个的时候,结束循环
            if(abroadCds.size() ==10&& unabroadCds.size()==10){
                break;
            }

            if(vo.isIsabroad()){//是否是海外的
                //是海外的
                if(abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                if(unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }

        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);


        //热门排行的获取
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getHotTop10();

        model.addAttribute("hotCds",hotCds);

        //主题的推荐
        //根据需求我们需要的是一个list集合,有多个MAP集合,MAP的key的是攻略主题的名字,value是一个集合,里面包含了攻略主题的,目的地,他们之前的联系是攻略明细
        List<Map<String,Object>> themeCds=strategyTemplateSearchService.getThemeCommend();

        model.addAttribute("themeCds",themeCds);


        //查询条件

        //国内条件 :通过省份id省份名 分
        List<StatisVo> chinas=strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        ;model.addAttribute("chinas",chinas);
        //国外条件 通过国家id国家名 分
        List<StatisVo> abroads=strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);
        //主题条件:通过主题id 主题名 分
        List<StatisVo> themes=strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);

        //hotCds

        return "strategy/index";

    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") SearchQueryObject qo){

    model.addAttribute("page",strategyTemplateSearchService.query(qo));
        return "strategy/searchPageTpl";
    }



    //攻略列表
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
         Destination dest   =toasts.remove(toasts.size() - 1);
         model.addAttribute("toasts",toasts);

         model.addAttribute("dest",dest);

        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags",list);

        //分页
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";

    }




    //通过目的地查看一个攻略
    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);//攻略明细
        StrategyContent content = strategyDetailService.getContent(id);//攻略的内容
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);
        //阅读量+1
        //参数1是攻略的id ,参数二是增加的数量
        strategyStatisRedisService.viewNumIncrease(id,1);


        //返回阅读数量,是新增的
        model.addAttribute("vo",strategyStatisRedisService.getStrategyStatisVo(id));

        //热门排行分数加一 :浏览数+评论数
        strategyStatisRedisService.addHotScore(RedisKeys.STRATEGY_STATIS_VO.join(id.toString()),1);
        return "strategy/detail";

    }


    //添加评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){//一个参数是封装的类型,一个是登录的账号
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(comment);


        //当前攻略的评论数 ,先存数据库,后面再redis实现
       /* StrategyDetail detail = strategyDetailService.get(comment.getDetailId());//能攻略明细的id
        detail.setReplynum(detail.getReplynum()+1);
        strategyDetailService.updateReplynum(detail.getId(),detail.getReplynum());*/
       //增加一条评论数量
       strategyStatisRedisService.replyNumIncrease(comment.getDetailId(),1);
        int replynum = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId()).getReplynum();

        //热门排行分数加一 :浏览数+评论数
        strategyStatisRedisService.addHotScore(RedisKeys.STRATEGY_STATIS_VO.join(comment.getDetailId().toString()),1);


        return AjaxResult.SUCCESS.addData(replynum);
    }

    //评论列表的显示
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    //攻略评论的点赞的处理
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp (String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }


    //评论的判断
    @RequestMapping("/commmentLogin")
    @ResponseBody
    public Object commmentLogin(HttpServletRequest request){
        String token = CookieUtil.getToken(request);
        //没登录
        if (token ==null){
            return AjaxResult.SUCCESS;
        }
        return AjaxResult.FAIL;
    }

    //收藏的新增和取消
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
            AjaxResult result=null;
            //表示没登陆，返回错误的代码 和错误信息
            if(userInfo==null){
                result=new AjaxResult(false,"请先登录，才能收藏");
                result.setCode(102);
                return result;
            }

        //收藏和取消收藏的实现
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
            //ret为true是收藏,false是取消收藏,收藏成功就加一分,失败就减一分
        strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),ret?1:-1);


            //返回结果，如果为成功就加上数量,,如果是取消收藏,也返回收藏的数量，但是错误结果为空
        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStrategyStatisVo(sid));

    }



    //攻略 顶的操作
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        AjaxResult result=null;
        //表示没登陆，返回错误的代码 和错误信息
        if(userInfo==null){
            result=new AjaxResult(false,"请先登录，顶哦");
            result.setCode(102);
            return result;
        }

        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        //如果ret为ture,代表为顶成功,我们需要加一分,false表示顶过了,不用加分
        if(ret){
            //第一个是value的值,第二个是增加的分数
            strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),1);
        }


        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStrategyStatisVo(sid));

    }



}
